<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="参与人手机号" prop="phoneNum">
        <el-input v-model="queryParams.phoneNum" placeholder="请输入参与人手机号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId==undefined&&queryParams.planId==undefined">-->
    <!--        <router-link :to="'/property/survey'" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId!=undefined && eventType == 1">-->
    <!--        <router-link :to="'event/check/plan'" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId!=undefined && eventType == 2">-->
    <!--        <router-link :to="'/event/patrol/task?workId='+queryParams.eventWorkId" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId!=undefined && eventType == 3">-->
    <!--        <router-link :to="'/event/clean/plan'" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId!=undefined && eventType == 4">-->
    <!--        <router-link :to="'/event/eliminator/plan'" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5" v-if="queryParams.eventWorkId!=undefined && eventType == 5">-->
    <!--        <router-link :to="'/event/maintenance/plan'" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->

    <el-col :span="1.5">
      <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                 v-hasPermi="['module:survey_record:export']">导出
      </el-button>
    </el-col>
<!--    <el-col :span="1.5">-->
<!--      <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleShengcheng"-->
<!--                 v-hasPermi="['module:survey_record:export']">生成-->
<!--      </el-button>-->
<!--    </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <!--      <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">-->
    <!--          <el-table-column type="selection" width="55" align="center" />-->
    <!--                  <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>-->
    <!--                  <el-table-column label="问卷id" align="center" prop="surveyId" />-->
    <!--                  <el-table-column label="物业id" align="center" prop="propertyId" />-->
    <!--                  <el-table-column label="小区id" align="center" prop="estateId" />-->
    <!--                  <el-table-column label="执行的任务id" align="center" prop="eventTaskInfoId" />-->
    <!--                  <el-table-column label="业主微信id" align="center" prop="userId" />-->
    <!--                  <el-table-column label="用户昵称" align="center" prop="nickName" />-->
    <!--                  <el-table-column label="手机号" align="center" prop="phoneNum" />-->
    <!--          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
    <!--              <template slot-scope="scope">-->
    <!--                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
    <!--                             v-hasPermi="['system:record:edit']">修改</el-button>-->
    <!--                  <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
    <!--                             v-hasPermi="['system:record:remove']">删除</el-button>-->
    <!--              </template>-->
    <!--          </el-table-column>-->
    <!--      </el-table>-->

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="参与人" align="center" prop="nickName"/>
      <el-table-column label="参与人手机号" align="center" prop="phoneNum"/>
      <el-table-column label="所在小区" align="center" prop="estateName"/>
      <el-table-column label="房屋户号" align="center" prop="roomName" v-if="this.queryParams.eventWorkId==undefined"/>
      <el-table-column label="计划名称" align="center" prop="planName" v-if="this.queryParams.eventWorkId!=undefined"/>
      <el-table-column label="是否异常" align="center" prop="yichang">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.yichang === 0" style="cursor: pointer" :type="''">正常</el-tag>
            <el-tag v-if="scope.row.yichang === 1" style="cursor: pointer" :type="''">异常</el-tag>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="提报时间" align="center" prop="createTime"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <router-link :to="'/property/survey/info?recordId=' + scope.row.id" class="link-type">-->
          <!--            <el-button size="mini" type="text" icon="el-icon-edit" v-if="queryParams.surveyId==93" @click="downloadPhoto(scope.row)"-->
          <!--                       v-hasPermi="['module:survey_record:edit']">下载图片</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:survey_record:edit']">上报调查内容
          </el-button>
          <!--          </router-link>-->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-table v-loading="loading" :data="recordListInfo" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="问卷内容" align="center" prop="question" />
        <el-table-column label="回答内容" align="center" prop="answerValue">
          <template slot-scope="scope">
            {{scope.row.answerValue}}
            <img :src="scope.row.imgValue" alt="点击打开" width="300px" @click="tupian(scope.row)" v-if="scope.row.imgValue!=null">
<!--            <a :href="scope.row.imgValue" style="color: #42b983" target="_blank" v-if="scope.row.imgValue!=null">-->
<!--              <img :src="scope.row.imgValue" alt="点击打开" class="el-avatar">-->
<!--            </a>-->
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>


    <el-dialog title="照片" :visible.sync="openImg" :width="dialogWidth" append-to-body>
      <img :src=this.imgUrl @load="onLoadImg" :width="boxWidth"/>
    </el-dialog>


  </div>
</template>

<script>
import {listRecord, getRecord, listWorkRecord, exportRecord, listPlanRecord,listRecordShengcheng} from "@/api/module/survey/record";
import { listRecordInfo, listRecordInfos, } from "@/api/module/survey/info";
export default {
  name: "Record",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dialogWidth:undefined,
      boxWidth:undefined,
      // 总条数
      total: 0,
      // 问卷调查的用户填报数据表格数据
      recordList: [],
      recordListInfo: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openImg:false,
      eventType: undefined,
      imgUrl:undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: undefined,
        surveyId: undefined,
        phoneNum: undefined,
        eventWorkId: undefined,
      },
      queryParamInfo: {
        pageNum: 1,
        pageSize: 100,
        recordId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.queryParams.surveyId = this.$route.query.surveyId ? this.$route.query.surveyId : 0;
    this.queryParams.eventWorkId = this.$route.query.eventWorkId ? this.$route.query.eventWorkId : undefined;
    this.queryParams.planId = this.$route.query.planId ? this.$route.query.planId : undefined;
    this.eventType = this.$route.query.eventType ? this.$route.query.eventType : undefined;
    this.getList();
  },
  methods: {
    /** 查询问卷调查的用户填报数据列表 */
    getList() {
      this.loading = true;
      // listRecord(this.queryParams).then(response => {
      //     this.recordList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      // });
      console.log(this.queryParams.eventWorkId);
      if (this.queryParams.eventWorkId != undefined) {
        listWorkRecord(this.queryParams).then(response => {
          this.recordList = response;
          this.loading = false;
        });
      } else if (this.queryParams.planId != undefined) {
        listPlanRecord(this.queryParams).then(response => {
          this.recordList = response;
          this.loading = false;
        });
      } else {
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    handleShengcheng(){
      listRecordShengcheng(this.queryParams).then(response => {
        this.recordList = response;
        this.loading = false;
      });
    },
    onLoadImg(e) {
      var img = e.target;
      var width = 0;
      if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
        width = img.width;
      }
      // if ((img.height > img.width) &&  width > 370) {
      //     width = 370
      // } else if (width > 600) {
      //     width = 600
      // }
      this.boxWidth= width + 'px';
      this.dialogWidth = width + 40 + 'px';
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        surveyId: undefined,
        propertyId: undefined,
        estateId: undefined,
        eventTaskInfoId: undefined,
        userId: undefined,
        nickName: undefined,
        phoneNum: undefined,
        createTime: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问卷调查的用户填报数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.queryParamInfo.recordId = row.id;
      listRecordInfos(this.queryParamInfo).then(response => {
        this.recordListInfo = response;
        this.totalInfo = response.total;
        this.loading = false;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    tupian(row){
      this.imgUrl = row.imgValue;
      this.openImg = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除问卷调查的用户填报数据编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有问卷调查的用户填报数据数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
